# -*- coding: utf-8 -*-
# @Author   : niejiali
# @Time     : 2019/1/4 10:44
# @File     : trade.py
# @Software : PyCharm
from collections import namedtuple

from app.view_models.book import BookViewModel


class TradeInfo:
    def __init__(self, trades):
        self.total = 0
        self.trades = []
        self._parse(trades)

    def _parse(self, trades):
        self.total = len(trades)
        self.trades = [self._map_to_trade(gift) for gift in trades]

    def _map_to_trade(self, single):
        return dict(
            user_name=single.user.nickname,
            time=single.create_time,  # .strftime('%Y-%m-%d'),
            id=single.id
        )


Trades = namedtuple('MyGift', ['id', 'book', 'wishes_count'])


class MyTrades:

    def __init__(self, trades_of_mine, trade_count_list):
        self.trades = []

        self.__trades_of_mine = trades_of_mine
        self.__trade_count_list = trade_count_list

        self.trades = self.__parse()

    def __parse(self):
        temp_trades = []
        for trade in self.__trades_of_mine:
            my_trade = self.__matching(trade)
            temp_trades.append(my_trade)
        return temp_trades

    def __matching(self, trade):
        count = 0
        for trade_count in self.__trade_count_list:
            if trade.isbn == trade_count['count']:
                count = trade_count['count']
        my_gift = Trades(trade.id, BookViewModel(trade.book), count)
        return my_gift
